package com.hilaver.dzmis.dao;

import java.util.List;

import org.hibernate.Query;

import com.hilaver.dzmis.accesscontrol.AcAction;
import com.hilaver.dzmis.accesscontrol.AcUser;
import com.hilaver.dzmis.accesscontrol.AcUserAction;

public class AcUserDAO extends BaseHibernateDAO {

	public List<AcUserAction> getAcUserActionListFrom(Integer userId,
			Integer actionId) throws Exception {
		String hql = "from " + AcUserAction.class.getName()
				+ " where acUser.id = ? and acAction.id = ?";
		try {
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, userId);
			queryObject.setParameter(1, actionId);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public List<AcUserAction> getAcUserActionByListFilters(String sort, String order, String[] filters) throws Exception {
		String hql = "from " + AcUserAction.class.getName()
				+ getWhereStatement(filters) + " order by " + sort + " "
				+ order;
		try {
			Query queryObject = getSession().createQuery(hql);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<AcUser> getAcUser(String userName, String pwd) throws Exception{
		String hql = "from " + AcUser.class.getName() + " where userName = ? and userPwd = ?";
		try {
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, userName);
			queryObject.setParameter(1, pwd);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<AcAction> getAcAction(String uri) throws Exception{
		String hql = "from " + AcAction.class.getName() + " where actionUrl = ?";
		try {
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, uri);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
